using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace TmxUtil
{
    public class UnlocalizedLogWriter
    {
        public static void Analize(Solution solution, string cultureName, TextWriter w)
        {
            foreach (Project p in solution.Projects)
            {
                bool hasProject = false;
                ResXCollection pc = new ResXCollection(p.ResXList);
                string[] resKeys = pc.GetKeys();
                foreach(string resKey in resKeys)
                {
                    bool hasResource = false;
                    ResX[] subList = pc.Find(resKey);
                    ResXItemCollection items = new ResXItemCollection(subList);
                    ResXBrowser browser = new ResXBrowser(items);
                    foreach (string gk in browser.GlobalKeys)
                    {
                        string localKey = browser.GetLocalKey(gk);
                        ResXItem target = browser.GetLocalization(gk, cultureName);
                        if (target == null)
                        {
                            ResXItem source = browser.GetLocalization(gk, string.Empty);
                            string sourceText = source == null ? null : source.Value;

                            if (source != null && !(sourceText != null && sourceText.Trim().Length == 0))
                            {
                                if (!hasProject)
                                {
                                    hasProject = true;
                                    w.WriteLine("Project {0}", p.Name);
                                }

                                if (!hasResource)
                                {
                                    hasResource = true;
                                    w.WriteLine("  {0}", resKey);
                                }


                                w.WriteLine(
                                    "    {0}: {1}", 
                                    localKey,
                                    sourceText == null ? "(no default)" : ResXStrings.GetDigest(sourceText) );
                            }
                        }
                    }


                }

                if (hasProject)
                    w.WriteLine();
            }
            
        }
    }
}
